/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.String;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Gebruiker;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author woutermkievit
 */
public class LoginController extends HttpServlet {

    SessionFactory sessionFactory;
    private Gebruiker user;
    String userEmail, userPassword;
    private HttpSession sessie;
    MenuHandler menuHandler;

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, NullPointerException {
        
        Session session = SchoolContentManager.getSessionFactory().getCurrentSession(); //get the session from the sessionFactory
        Transaction transaction = session.beginTransaction(); //start the transaction

        //session.update(user); //update existing user
        //session.save(user); //save new user
        //transaction.commit(); //commit user to data
        System.out.println("test");
        sessie = request.getSession();
        try {
            userEmail = request.getParameter("email");
            userPassword = request.getParameter("password");

            String getUserSQL = "SELECT * FROM gebruiker g WHERE lower(g.email) = lower('" + userEmail + "') AND g.password = '" + userPassword + "'";
            ArrayList<Gebruiker> users = (ArrayList) session.createSQLQuery(getUserSQL).list(); //Get all information from user by using email and password

            PrintWriter out = response.getWriter();
            out.println(userEmail);
            Iterator it = users.iterator();
            if (it.hasNext()) {
                Object[] row = (Object[]) it.next();
                user = new Gebruiker((String) row[0], (String) row[1], (String) row[2], (String) row[3], (String) row[4], (String) row[5]);
                sessie.setAttribute("user", (String)row[0]);
                sessie.setAttribute("cuser", user);
                menuHandler = new MenuHandler(sessie);

                menuHandler.menuAdd("selectCourse", "Course Catalogue");
                menuHandler.menuAdd("logout","Logout");
                setTypeuser();
                out.println("You're logged in");
                
        request.setAttribute("content", "homepage.jsp");

            } else {
                sessie.setAttribute("fout", "The e-mailadress or password are incorrect.");
            }

            transaction.commit();
        } catch (NullPointerException exc) {
            sessie.setAttribute("fout", "Login has failed. Please try again.");
            System.err.println(exc);
        }
        response.sendRedirect("index");
    }
    private void setTypeuser() {
        
        Session session = SchoolContentManager.getSessionFactory().getCurrentSession(); //get the session from the sessionFactory
        
        String gebruikerCode=(String)sessie.getAttribute("user");
        String getUserSQL = "SELECT * FROM teacher t WHERE t.uCode='"+gebruikerCode+"'";
            ArrayList<Object[]> users = (ArrayList) session.createSQLQuery(getUserSQL).list(); //Get all information from user by using email and password

            Iterator it = users.iterator();
            sessie.setAttribute("isAdmin", false);
            if (it.hasNext()) {
                Object[] row = (Object[]) it.next();
                if(row[0].equals(true)){
                    sessie.setAttribute("isAdmin", true);
                    System.out.println("blablabla\t\tblablabla");
                    menuHandler.addAdminOptions();
                    
                }
                sessie.setAttribute("isTeacher", true);
            } else {
                sessie.setAttribute("isTeacher", false);
            }

    }
}
